using System;

namespace Prologis.ShifterNT.Tokenizer
{
    public class SeparatorTokenizer : ITokenizer
    {
        public SeparatorTokenizer()
        {
            //###########################
            string s = "";
        }

        public string FieldConfigPath { get; set; }

        StringSplitOptions stringSplitOptions = StringSplitOptions.None;
        public StringSplitOptions StringSplitOptions
        {
            get { return stringSplitOptions; }
            set { stringSplitOptions = value; }
        }

        string separators;
        string[] separatorArray;

        public string Separators
        {
            get { return separators; }
            set
            {
                separators = value;
                separatorArray = separators.Split(new char[] { '+' });
            }
        }


        public string Separator
        {
            set
            {
                separators = value;
                separatorArray = new String[] { value };
            }
        }




        FieldList fields;
        public FieldList Fields
        {
            get { return fields; }
            set { fields = value; }
        }


        public FieldList Tokenize(string sourceId, object data)
        {
            FieldList list = (FieldList)fields.Clone();
            list.HasValues = false;
            string[] token = data.ToString().Split(separatorArray, this.StringSplitOptions);
            if (list.Source.Count > 1)
            {
                if (list.Source.ContainsKey("Main"))
                {
                    if (token.Length > list.Source["Main"].Count)
                    {
                        // letzten Delimiter aus Quellendatei entfernen
                        data = data.ToString().Remove(data.ToString().Length - 1);
                        token = data.ToString().Split(separatorArray, this.StringSplitOptions);
                    }
                    int tokenPos = 0;
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i].Source == sourceId)
                        {
                            list[i].Value = token[tokenPos] != String.Empty ? token[tokenPos] : null;
                            tokenPos++;
                        }
                    }
                }
            }
            else
            {
                // funktioniert nur wenn Werte aus eine DataSorce kommen ("Main")           
                for (int i = 0; i < list.Count; i++)
                {

                    try
                    {
                        if (list[i].Source == sourceId)
                        {
                            list[i].Value = token[i] != String.Empty ? token[i] : null;
                        }
                    }
                    catch (Exception)
                    {
                        throw new Exception("Error: " + token[i - 1].ToString() + "[ listCount : " + list.Count.ToString() + "/TokenCount: " + token.Length.ToString());
                    }
                }
            }
            list.HasValues = true;

            return list;
        }


        
        
    }
}
